package com.mangowang.module.main.ui;

/**
 * 文件名 : EaringDetailFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:47
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseFragment;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Order;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.ShopOrderAdapter;
import com.mangowang.module.main.bean.ShopOrderBean;
import com.mangowang.module.main.dialog.SnRecycleDialog;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import kale.ui.view.dialog.EasyDialog;

public class ShopRecordFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R2.id.fragment_recycleView)
    RecyclerView fragmentRecycleView;


    private int page = 1; // 当前页数
    private int position;  // 当前viewPage的位置 0:提现消息 1:奖励消息 2:担保消息 3:系统消息
    private List<ShopOrderBean> adapterDates = new ArrayList<>(); // 提现消息
    private ShopOrderAdapter mAdapter; // 提现消息适配器
    private String type;
    private int pagesize = 10;
    private int totals = 0;

    public static ShopRecordFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        ShopRecordFragment messageTypeFragment = new ShopRecordFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_chu_run;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        NetworkManager.INSTANCE.post(Apis.order_list, page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                List<Order.order_detail> lists = Order.order_list.parseFrom(bytes).getOrderListList();
                for (Order.order_detail temp: lists) {
                    ShopOrderBean titleBean = new ShopOrderBean();
                        titleBean.setTitle_item(temp);
                        mAdapter.addData(titleBean);
                    for (Order.order_info_item item: temp.getOrderInfoList()) {
                        ShopOrderBean itemBean = new ShopOrderBean();
                        itemBean.setItemType(1);
                        itemBean.setItem(item);
                        mAdapter.addData(itemBean);
                    }
                    ShopOrderBean lineBean = new ShopOrderBean();
                    lineBean.setItemType(2);
                    mAdapter.addData(lineBean);
                }
                int size = lists.size();
                totals = totals  + size;
                if (size < 10) {
                    mAdapter.getLoadMoreModule().loadMoreEnd(true);  // 没有分页操作
                } else {
                    mAdapter.getLoadMoreModule().loadMoreComplete();  // 没有分页操作
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                if (!TextUtils.isEmpty(errorMessage)) showToast(errorMessage);
            }
        });

    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        fragmentRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        fragmentRecycleView.setAdapter(mAdapter = new ShopOrderAdapter(mContext, adapterDates));
        // 每个item的点击事件
//        mAdapter.setEmptyView(R.layout.recycle_empty);
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            page = totals / pagesize + 1;
            loadData();
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {

            if (mAdapter.getData().get(position).getItemType()==1)
            {

                SnRecycleDialog.Builder builder = new SnRecycleDialog.Builder(mContext);

                List<String> snData = Arrays.asList(  mAdapter.getData().get(position).getItem().getSnCode().split(","));
                builder.setDate(snData).setMess(mAdapter.getData().get(position).getItem().getGoodsName()+" "+mAdapter.getData().get(position).getItem().getGoodsSerial(),mAdapter.getData().get(position).getItem().getNum()+"台");
                EasyDialog build = builder.build();
                build.setCancelable(true);
                build.show(getFragmentManager());




            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
